package com.emeraldparser.util;

import com.google.common.base.Objects;
import com.google.common.collect.Table;

public final class ImmutableTableCell<R, C, V> implements Table.Cell<R, C, V> {

	private final R rowKey;
	private final C columnKey;
	private final V value;

	private ImmutableTableCell(R rowKey, C columnKey, V value) {
		this.rowKey = rowKey;
		this.columnKey = columnKey;
		this.value = value;
	}

	public static <R, C, V> ImmutableTableCell<R, C, V> create(R rowKey, C columnKey, V value) {
		return new ImmutableTableCell<R, C, V>(rowKey, columnKey, value);
	}

	@Override
	public R getRowKey() {
		return rowKey;
	}

	@Override
	public C getColumnKey() {
		return columnKey;
	}

	@Override
	public V getValue() {
		return value;
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(rowKey, columnKey, value);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		
		if (obj == null) {
			return false;
		}
		
		if (!(obj instanceof Table.Cell<?, ?, ?>)) {
			return false;
		}
		
		Table.Cell<?, ?, ?> other = (Table.Cell<?, ?, ?>) obj;
		
		return Objects.equal(rowKey, other.getRowKey())
		    && Objects.equal(columnKey, other.getColumnKey())
		    && Objects.equal(value, other.getValue());
	}

	@Override
	public String toString() {
		return "{(" + rowKey + "," + columnKey + "): " + value + "}";
	}

}
